HTInterface = {};
HTInterface.loading = {txt: 'Loading...'};
HTInterface.loading.show = function(InsteadTxt, autoHidden) {
	if(InsteadTxt) {
		HTInterface.loading.txt = InsteadTxt;
		if(HTInterface.loadingInstance) { HTInterface.loadingInstance.remove(); delete HTInterface.loadingInstance; }
	}
	if(!HTInterface.loadingInstance){
		HTInterface.loadingInstance = $('<div class="ajax-loading-box" ><div id="ajax_loading_message" class="ajax-inner-loading-box">'+ HTInterface.loading.txt +'</div></div>');
		$('body').append(HTInterface.loadingInstance);
	}else{ 
		   HTInterface.loadingInstance.show();
    }
	if(autoHidden) setTimeout(HTInterface.loading.hide, autoHidden);
};
HTInterface.loading.hide = function() {
	HTInterface.loadingInstance.hide();
};

HTGetFiles = {cssLoaded : { }, scriptLoaded : { }, htmLoaded : {}};
HTGetFiles.resources = "http://192.168.1.99:9090/htprojects/client/public/";
HTGetFiles.Script = function(urlJs, callbacks) {
	
    if (!HTGetFiles.scriptLoaded[urlJs]) {
    	$.getScript(urlJs)
    	.done(function(script, textStatus) {
    		  HTGetFiles.scriptLoaded[urlJs] = "__loaded__";
    	      if(callbacks) callbacks();
    	})
    	.fail(function(jqxhr, settings, exception) {
    	    console.error( "Triggered ajaxError handler." );
    	})
    } else {
        if (callbacks) callbacks();
    }
};

HTGetFiles.CSS = function(urlCss, callbacks) {
    if (!HTGetFiles.cssLoaded[urlCss]) {

        var stylesheet = document.createElement('link');
        stylesheet.href = urlCss;
        stylesheet.rel = 'stylesheet';
        stylesheet.type = 'text/css';
        document.getElementsByTagName('head')[0].appendChild(stylesheet);
        HTGetFiles.cssLoaded[urlCss] = "__loaded__";
        if (callbacks) callbacks();

    } else {
        if (callbacks) callbacks();
    }
};
/* Article */
HTCloneArticle = {};
HTCloneArticle.setup = function(fnstart){
   var params = {header: "", content: "", source: ""};
    //genk.vn:
    HTCloneArticle['genk.vn'] = function(fncomplete){
    	
		 params.header = $(".news-showtitle h1").html();
		 params.content = $(".content").html();
		 params.source = "";
		 {
			$(".note a").remove();
			params.source = $(".note").html().replace("|","");;
			params.source = params.source.trim();
		 }
		fncomplete(params);
    };
   //shopdunk.vn:
    HTCloneArticle['shopdunk.vn'] = function(fncomplete){
		 params.header = $(".it_row_center_t h1").html();
		 params.content = $(".it_row_center_te").html();
		 params.source = "";  
		 {
		 
		 }
		fncomplete(params);
    };
    if(fnstart) fnstart();
};

HTCloneArticle.setupMenu = function(){
	
	HTGetFiles.CSS(HTGetFiles.resources + '/thirdparty/contextmenu/css/jquery.contextmenu.css');
	HTGetFiles.Script(HTGetFiles.resources + '/thirdparty/contextmenu/js/jquery.contextmenu.js', function () {
	    $('body').contextPopup({
	        title: 'Editer:',
	        items: [{
	                    label: 'Temp Saved',
	                    icon: HTGetFiles.resources + 'thirdparty/contextmenu/icons/save.png',
	                    action: function () {
	                    	HTCloneArticle.tempSaved();
	                    }
	                }, {
	                    label: 'Actually Saved',
	                    icon: HTGetFiles.resources + 'thirdparty/contextmenu/icons/sync.png',
	                    action: function () {
	                    	HTCloneArticle.actuallySaved();
	                    }
	                },
	                null, 
	                {
	                    label: 'Reload currenly page',
	                    icon: HTGetFiles.resources + 'thirdparty/contextmenu/icons/reload.png',
	                    action: function () {
	                        window.location.reload();
	                    }
	            }
	        ]
	    }).bind('click', function(){
	    	$('.ht-menu').remove();
	    });
	});
};

HTCloneArticle.tempSaved = function(){
	
};

HTCloneArticle.actuallySaved = function(){
	
};


/* ComedyStories  */
HTComedyStories = {};
HTComedyStories.data = {};
HTComedyStories.setup = function(fnstart){
    if(fnstart) fnstart();
};

HTComedyStories.setupMenu = function(){
	
	HTGetFiles.CSS('https://ht-projects-all.googlecode.com/svn-history/r4/jquery.contextmenu.css');
	HTGetFiles.Script('https://ht-projects-all.googlecode.com/svn-history/r4/jquery.contextmenu.js', function () {
	    $('body').contextPopup({
	        title: 'Post Stories:',
	        items: [
	                {
	                    label: 'Copy Title',
	                    action: function () {
	                    	HTComedyStories.data['Title'] = window.getSelection().getRangeAt(0).commonAncestorContainer.data;
	                    	HTInterface.loading.show("Title content copying...", 1000);
	                    }
	                }, {
	                    label: 'Copy Body',
	                    action: function () {
	                    	if(!HTComedyStories.data['Title']){
	                    		alert("You must copy [Title] first!"); return;
	                    	}
	                    	{
	                    		var txtSelection = window.getSelection().getRangeAt(0).commonAncestorContainer.data;
	                    		if(txtSelection){
	                    			$content = txtSelection;
	                    		}else {
	                    			$content = window.getSelection().extentNode.outerHTML;
	                    		}
	                    	}
	                    	HTComedyStories.data['Body'] = $content;
	                    	HTInterface.loading.show("Data Saving...", 1200);
	                    	console.log("Data Just saved:", HTComedyStories.data);
	                    	window.localStorage.setItem(HTComedyStories.pathname + "-" + new Date().getTime(), JSON.stringify(HTComedyStories.data));
	                    	HTComedyStories.data = {};
	                    }
	                },
	                null
	                , {
	                    label: 'Actually Saved',
	                    icon: HTGetFiles.resources + 'thirdparty/contextmenu/icons/sync.png',
	                    action: function () {
	                    	HTComedyStories.actuallySaved();
	                    }
	                },
	                null, 
	                {
	                    label: 'Reload currenly page',
	                    icon: HTGetFiles.resources + 'thirdparty/contextmenu/icons/reload.png',
	                    action: function () {
	                        window.location.reload();
	                    }
	            }
	        ]
	    }).bind('click', function(){
	    	$('.ht-menu').remove();
	    });
	    
	    $("img").hover(function(){
	    	HTComedyStories.imgSeleted = $(this).attr('src');
	    }, function(){
	    	delete HTComedyStories.imgSeleted;
	    });
	});
};

HTComedyStories.actuallySaved = function(){
	
};

(function (host, pathname) {
	
	HTComedyStories.setup(function(){
		HTComedyStories.host = host;
		HTComedyStories.pathname = pathname;
		HTComedyStories.setupMenu();
	});
	/*
	HTCloneArticle.setup(function(){
		HTCloneArticle.host = host;
		HTCloneArticle.pathname = pathname;
		HTCloneArticle.setupMenu();
	}); */
	
})(window.location.host, window.location.pathname);

// Inject files:
/*  var fileref=document.createElement('script')
	fileref.setAttribute("type","text/javascript")
	fileref.setAttribute("src", "//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js");
	document.getElementsByTagName("head")[0].appendChild(fileref); */